package com.entrogy.service.impl;

import com.entrogy.bean.*;
import com.entrogy.dao.MessageMapper;
import com.entrogy.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author 许红松
 * @date 2019/5/30 0030 上午 11:18
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;//信息发送dao接口

    /**
     * 获取类型
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getChanggeType(Map<String, Object> map) {
        return messageMapper.getChanggeType(map);
    }

    ;

    /**
     * 新增一个信息
     *
     * @param messageEntity
     * @return
     */
    public int insertMessage(MessageEntity messageEntity) {
        messageMapper.insertMessage(messageEntity);
        return messageEntity.getMessage_id();
    }

    /**
     * 获取年级班级等
     *
     * @return
     */
    @Override
    public List<ClassEntity> getClassList(Map<String, Object> map, boolean isClassLeader, boolean isGradeLeader) {
        List<ClassEntity> list = messageMapper.getClassList(map);
        if (list != null && !isClassLeader && !isGradeLeader) {
//            list.add(new ClassEntity().getMyClassEntity(1,"高中部"));
//            list.add(new ClassEntity().getMyClassEntity(2,"初中部"));
//            list.add(new ClassEntity().getMyClassEntity(3,"小学部"));
            list.add(0, new ClassEntity().getMyClassEntity(4, "全校"));
        }
        return list;
    }

    /**
     * 获取任课老师的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<String> getClassTeacherList(Map<String, Object> map) {
        String a = map.get("classId").toString();
        if (map.containsKey("classId")) {
            if (map.get("classId").equals("1") || map.get("classId").equals("2") || map.get("classId").equals("3") || map.get("classId").equals("4")) {
                map.remove("classId");
            }
        }
        List<CourseEntity> list = messageMapper.getClassTeacherList(map);
        List<CourseEntity> list1 = new ArrayList<>();
        for (CourseEntity courseEntity : list) {
            if (a.equals("1")) {////高中
                if (courseEntity.getClassId().startsWith("11")) {
                    list1.add(courseEntity);
                }
            }
            if (a.equals("2")) {//初中
                if (courseEntity.getClassId().startsWith("12")) {
                    list1.add(courseEntity);
                }
            }
            if (a.equals("3")) {//小学
                if (courseEntity.getClassId().startsWith("13")) {
                    list1.add(courseEntity);
                }
            }
            if (a.equals("4")) {//全校
                list1.add(courseEntity);
            }
            if (!a.equals("4")) {
                if (!a.substring(4, 5).equals("0")) {//代表班级
                    if (courseEntity.getClassId().startsWith(a.substring(0, 5))) {
                        list1.add(courseEntity);
                    }
                } else {
                    if (courseEntity.getClassId().startsWith(a.substring(0, 4))) {
                        list1.add(courseEntity);
                    }
                }

            }
        }
        List<String> strsToList1 = new ArrayList<>();
        if (list1 != null) {
            String teacherid = "";
            StringBuffer sf = new StringBuffer();
            for (CourseEntity courseEntity : list1) {
                teacherid = sf.append(courseEntity.getTeacherId()).append(",").toString();
            }
            try {
                strsToList1 = Arrays.asList(teacherid.split(","));
                //去重
                HashSet h = new HashSet(strsToList1);
                strsToList1.clear();
                strsToList1.addAll(h);
            } catch (UnsupportedOperationException e) {

            }

        }
        return strsToList1;
    }

    /**
     * 查询年级主任  班主任或者职员等的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<UserEntity> getpTypeList(Map<String, Object> map) {
        // 如果是年级性教师列表
        int isGrade = 0;
        // 如果是班级性教师列表
        int isClass = 0;
        // 如果是全校性教师列表
        int isSchool = 0;
        if (map.get("classId").equals("4")) {
            isSchool = 1;
        } else if (((String) map.get("classId")).substring(4, 5).equals("0")) {
            isGrade = 1;
        } else {
            isClass = 1;
        }
        map.put("isGrade", isGrade);
        map.put("isClass", isClass);
        map.put("isSchool", isSchool);
        List<UserEntity> list = messageMapper.listUserByType(map);
        return list;
    }

    /**
     * 查询某些任课老师  比如某个年级班级等
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getUsersbyBatchId(Map<String, Object> map) {
        return messageMapper.getUsersbyBatchId(map);
    }

    /**
     * 查询家长信息
     *
     * @param map
     * @return
     */
    @Override
    public List<ParentVisitorEntity> getParentsbyBatchId(Map<String, Object> map) {
        return messageMapper.getParentsbyBatchId(map);
    }


    /**
     * 获取年级班级等
     *
     * @return
     */
    @Override
    public List<ClassEntity> getClassList1(Map<String, Object> map) {
        return messageMapper.getClassList1(map);
    }

    /**
     * 根据模板id查询模板内容
     *
     * @param template_id
     * @return
     */
    @Override
    public Template getTemplateContentById(int template_id, int schoolCode) {
        return messageMapper.getTemplateContentById(template_id, schoolCode);
    }

    @Override
    public PubCountEntity getPubCountOpenidByPhone(String phone) {
        return messageMapper.getPubCountOpenidByPhone(phone);
    }

    @Override
    public Integer insertMessageRecord(MessageSendEntity messageSendEntity) {
        messageMapper.insertMessageRecord(messageSendEntity);
        return messageSendEntity.getId();
    }

}
